<template>
  <div class="my-watchlist-container">
    <div class="page-header">
      <h1 class="page-title">我的片单</h1>
      <div class="tab-bar">
        <button :class="{active: tab==='want'}" @click="tab='want'">想看</button>
        <button :class="{active: tab==='watched'}" @click="tab='watched'">已看</button>
      </div>
    </div>
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else>
      <div v-if="movies.length === 0" class="empty-state">
        <div class="empty-icon">🎬</div>
        <div class="empty-text">暂无{{ tab==='want' ? '想看' : '已看' }}电影</div>
      </div>
      <div v-else class="movie-list">
        <div v-for="movie in movies" :key="movie.id" class="movie-item">
          <img :src="processImageUrl(movie.poster) || 'https://via.placeholder.com/60x90'" class="movie-poster"/>
          <div class="movie-info">
            <h4 class="movie-title" @click="goToMovieDetail(movie.id)" style="cursor:pointer;color:#667eea;">{{ movie.title }}</h4>
            <div class="movie-meta">
              <span>{{ movie.releaseDate }}</span> / <span>{{ movie.genre }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue';
import axios from 'axios';
import { useAuthStore } from '@/store/authStore';
import { useRouter } from 'vue-router';
import { processImageUrl } from '@/utils/imageUtils';

const tab = ref('want'); // want/watched
const movies = ref([]);
const loading = ref(false);
const authStore = useAuthStore();
const router = useRouter();

const userId = authStore.user?.id || Number(localStorage.getItem('userId')) || 2;

async function fetchWatchlist() {
  loading.value = true;
  let type = tab.value === 'want' ? 1 : 2;
  try {
    // 获取收藏记录
    const res = await axios.get(`http://localhost:9527/favorite/user/${userId}/watchlist/${type}`);
    const favs = res.data || [];
    // 批量获取电影详情
    const movieIds = favs.map(f => f.movieId);
    if (movieIds.length === 0) {
      movies.value = [];
      loading.value = false;
      return;
    }
    const movieRes = await axios.get(`http://localhost:9527/movies`, { params: { ids: movieIds.join(',') } });
    // 这里假设后端支持批量ids查询，否则需循环请求
    movies.value = (movieRes.data || []).filter(m => movieIds.includes(m.id));
  } catch (e) {
    movies.value = [];
  } finally {
    loading.value = false;
  }
}

watch(tab, fetchWatchlist);
onMounted(fetchWatchlist);

function goToMovieDetail(id) {
  router.push({
    path: `/movie/${id}`,
    query: { from: 'my-watchlist' }
  });
}
</script>

<style scoped>
.my-watchlist-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 24px;
}
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tab-bar {
  display: flex;
  gap: 12px;
}
.tab-bar button {
  padding: 6px 18px;
  border: none;
  background: #f0f0f0;
  border-radius: 4px;
  cursor: pointer;
}
.tab-bar button.active {
  background: #667eea;
  color: #fff;
}
.movie-list {
  margin-top: 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 18px;
}
.movie-item {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px #eee;
  padding: 12px;
  width: 320px;
}
.movie-poster {
  width: 60px;
  height: 90px;
  object-fit: cover;
  border-radius: 4px;
  margin-right: 16px;
}
.movie-title {
  font-size: 18px;
  margin: 0 0 6px 0;
}
.movie-meta {
  color: #888;
  font-size: 14px;
}
.loading {
  text-align: center;
  margin: 40px 0;
}
.empty-state {
  text-align: center;
  margin: 40px 0;
}
.empty-icon {
  font-size: 40px;
  margin-bottom: 12px;
}
</style> 